package com.mindether.llyblog.controller;

import com.mindether.llyblog.dao.TagMapper;
import com.mindether.llyblog.pojo.ApiPageResponse;
import com.mindether.llyblog.pojo.Article;
import com.mindether.llyblog.pojo.Tag;
import com.mindether.llyblog.service.ArticleService;
import com.mindether.llyblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    @Value("${lly.blog.pagesize}")
    private int pageSize;

    /**
     * 显示首页，实际上返回所有文章第一页
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String showIndex( Model model){
        ApiPageResponse<Article> firstPageArticleList= articleService.findArticleByPage(1,pageSize);

        model.addAttribute("recommendArticle", articleService.recommendArticle());
        model.addAttribute("pageArticleList",firstPageArticleList);
        model.addAttribute("allTags",ArticleController.allTags);
        return "index";
    }

    /**
     * 分页查询所有文章
     * @param page
     * @param model
     * @return
     */
    @RequestMapping("/page/{page}")
    public String page( @PathVariable("page")Integer page, Model model){
        ApiPageResponse<Article> pageArticles= articleService.findArticleByPage(page,pageSize);

        model.addAttribute("recommendArticle", articleService.recommendArticle());
        model.addAttribute("pageArticleList",pageArticles);
        model.addAttribute("allTags",ArticleController.allTags);
        return "index";
    }

    @RequestMapping("/about-me")
    public String aboutMe( Model model){

        model.addAttribute("allTags",ArticleController.allTags);
        return "about";
    }


}
